package com.android.webttest.utils;



import androidx.annotation.NonNull;

import com.android.webttest.Application.MyApplication;
import com.android.webttest.Config.AppDatabase;
import com.android.webttest.model.tableModel.GoodFavorite;
import com.android.webttest.model.tableModel.GoodLike;
import com.android.webttest.model.tableModel.GoodShare;
import com.android.webttest.model.tableModel.SearchHistoryRecord;


import java.util.List;

public class RoomDBTools {
    // 获取数据库实例
    private static AppDatabase getDatabase() {
        return MyApplication.getDatabase();
    }

    /**
     * 插入一条搜索记录
     */
    public static void insertSearchHistory(@NonNull SearchHistoryRecord record) {
        getDatabase().searchHistoryDao().insert(record);
    }

    /**
     * 查询所有搜索记录（按时间倒序）
     */
    public static List<SearchHistoryRecord> getAllSearchHistory() {
        return getDatabase().searchHistoryDao().getAll();
    }

    /**
     * 清空所有搜索记录
     */
    public static void deleteAllSearchHistory() {
        getDatabase().searchHistoryDao().deleteAll();
    }

    /**
     * 按关键词查找是否存在（可选）
     */
//    public static SearchHistoryRecord findSearchRecordByQuery(String query) {
//        return getDatabase().searchHistoryDao().findByQuery(query);
//    }
    /**
     * 根据关键词查找是否存在并更新或插入记录
     */


    public static void updateOrInsertSearchHistory(String query) {
        SearchHistoryRecord record = getDatabase().searchHistoryDao().findByQuery(query);

        if (record != null) {
            // 记录存在，更新字段
            record.setRecent(true);
            record.setSearchCount(record.getSearchCount()+1);
            record.setUpdateTime(String.valueOf(System.currentTimeMillis())); // 假设 updateTime 是 long 类型
            getDatabase().searchHistoryDao().update(record);
        } else {
            // 记录不存在，插入新数据
            SearchHistoryRecord newRecord = new SearchHistoryRecord(query);
            getDatabase().searchHistoryDao().insert(newRecord);
        }
    }

    public static void updateEveryDaySearchHistory(){
        List<SearchHistoryRecord> historyList = getDatabase().searchHistoryDao().getAll();
        long threeDaysAgo = System.currentTimeMillis() - (3 * 24 * 60 * 60 * 1000); // 3天前的时间戳

        for (SearchHistoryRecord record : historyList) {
            try {
                long updateTime = Long.parseLong(record.getUpdateTime());

                if (updateTime < threeDaysAgo) {
                    record.setRecent(false);
                    getDatabase().searchHistoryDao().update(record);
                }
            } catch (NumberFormatException e) {
                // 忽略无法解析的时间格式
                e.printStackTrace();
            }
        }
    }

    public static void insertGoodLike(GoodLike goodLike){
        getDatabase().goodLikeDao().insert(goodLike);
    }

    public static GoodLike loadGoodLikeByGoodId(Integer goodId){
       return   getDatabase().goodLikeDao().loadGoodLikeByGoodId(goodId);
    }

    public static void deleteGoodLikeById(Integer id) {
        getDatabase().goodLikeDao().deleteGoodLikeById(id);
    }

    public static void insertGoodShare(GoodShare goodShare){
        getDatabase().goodShareDao().insert(goodShare);
    }

    public static void insertGoodFavorite(GoodFavorite goodFavorite){
        getDatabase().goodFavoriteDao().insert(goodFavorite);
    }


}
